#region License, Terms and Conditions
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
#endregion
using System;
using System.Web.Mvc;
using pesta.Data;
using pesta.Data.Model.Helpers;
using raya.Library.Helpers;
using raya.Models.login;

namespace raya.Controllers
{
    public class registerController : baseController
    {
        public ActionResult Index()
        {
            return View("register");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(string register_first_name, string register_last_name, string register_email, string register_password,
            string date_of_birth_day, string date_of_birth_month, string date_of_birth_year, string gender)
        {
            var loginUser = new login();
            if (Session["id"] != null)
            {
                return Redirect("/");
            }
            
            // check to see if all required fields are filled in
            if (String.IsNullOrEmpty(register_email) ||
                String.IsNullOrEmpty(register_password) ||
                String.IsNullOrEmpty(register_first_name) ||
                String.IsNullOrEmpty(register_last_name) ||
                String.IsNullOrEmpty(gender) ||
                String.IsNullOrEmpty(date_of_birth_month) ||
                String.IsNullOrEmpty(date_of_birth_day) ||
                String.IsNullOrEmpty(date_of_birth_year) ||
                !PHPExt.is_numeric(date_of_birth_month) ||
                !PHPExt.is_numeric(date_of_birth_day) ||
                !PHPExt.is_numeric(date_of_birth_year))
            {
                handleErrors("Fill in all fields to continue");
            }
            else
            {
                // check availability of the email addr used
                var dateOfBirth = new DateTime(int.Parse(date_of_birth_year), 
                                            int.Parse(date_of_birth_month),
                                            int.Parse(date_of_birth_day), 0, 0, 1);
                try
                {
                    // attempt to register this person, any error in registration will cause an exception
                    loginUser.registerPerson(register_email,
                                             register_password,
                                             register_first_name,
                                             register_last_name,
                                             (Person.Gender)gender.ToEnum<Person.Gender>(), 
                                             dateOfBirth);

                    // registration went ok, set up the session (and cookie)
                    Authenticate(register_email, register_password);


                    // and finally, redirect the user to his profile edit page
                    // don"t continue output, all we want is a redirection
                    return RedirectToAction("edit", "profile");
                }
                catch (Exception e)
                {
                    // something went wrong with the registration
                    handleErrors(e.Message);
                }
            }
            
            return View("register");
        }
    }
}